<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <!-- 放置页签 -->
        <el-tab-pane label="角色管理">
          <!-- 新增角色按钮 -->
          <el-row style="height: 60px">
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="dialogVisible = true"
            >新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table border="" :data="list">
            <el-table-column label="序号" type="index" width="120" />
            <el-table-column label="角色名称" width="240" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <el-button size="small" type="success">分配权限</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="editRole(row)"
                >编辑</el-button>
                <el-button
                  size="small"
                  type="danger"
                  @click="delRole(row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :current-page.sync="page.page"
            :page-size="page.pagesize"
            :total="page.total"
            @current-change="getRoleList"
          >/>
          </el-pagination></el-tab-pane>
        <el-tab-pane label="公司信息">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top: 50px">
            <el-form-item label="公司名称">
              <el-input disabled style="width: 400px" :value="companyInfo.name" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input disabled style="width: 400px" :value="companyInfo.companyAddress" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input disabled style="width: 400px" :value="companyInfo.mailbox" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input
                type="textarea"
                :rows="3"
                disabled
                style="width: 400px"
                :value="companyInfo.remarks"
              />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 这里用了sync，因为dialog组件再父组件里面，数据共用了 -->
    <el-dialog
      :title="roleForm.id ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="beforeClose"
    >
      <!-- before-close关闭之前的回调，只有自己身上点击❌的关闭能触发 -->
      <el-form
        ref="roleForm"
        label-width="80px"
        :model="roleForm"
        :rules="roleRules"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="beforeClose">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, delRole, addRole, getRoleDetail, updateRole, getCompanyInfo } from '@/api/setting'
export default {
  data() {
    return {
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      list: [],
      dialogVisible: false,
      roleForm: {
        name: '',
        description: ''
      },
      roleRules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      companyInfo: {}
    }
  },

  async created() {
    this.getRoleList()
    this.getCompanyInfo()
  },

  methods: {
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    beforeClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.dialogVisible = false
    },
    // 第一次获取，新增，更改，页数改变都调用，所以封装
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    async delRole(id) {
      // console.log(column)
      try {
        await this.$confirm('确定删除吗？')
        await delRole(id)
        this.$messagge.success('删除成功了')
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    async submit() {
      try {
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        this.getRoleList()
        this.$message.success('添加成功')
        this.beforeClose()
        // this.dialogVisible = false
      } catch (error) {
        console.log(error)
      }
    },
    async editRole(row) {
      this.roleForm = { ...row }
      this.roleForm.companyId = this.$store.state.user.userInfo.companyId
      const res = await getRoleDetail(row.id)
      this.roleForm.permIds = res.permIds
      this.dialogVisible = true
    }

  }
}
</script>

<style scoped lang="less"></style>
